<?php

class userActions extends sfActions
{
	public function executeRegister()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Handle the form submission
			$user = new sfGuardUser();
			$user->setUsername($this->getRequestParameter('email'));
			$user->setPassword($this->getRequestParameter('password'));
			$user->setIsActive(false);

                  $group = sfGuardGroupPeer::getTutorGroup();

			$usergroup = new sfGuardUserGroup();
                  $usergroup->setsfGuardUser($user);
			$usergroup->setsfGuardGroup($group);
			$usergroup->save();

			$this->setFlash('key', $user->getSalt());
			$email = $this->sendEmail('mail','sendActivation');

			return 'NewUser';
		}
	}

	public function handleErrorRegister()
	{
		return sfView::SUCCESS;
	}

	public function executeActivate()
	{
		$login = $this->getRequestParameter('login');
		$key = $this->getRequestParameter('key');
		$user = sfGuardUserPeer::retrieveByUsername($login, false);

		if ($user)
		{
			$salt = $user->getSalt();
			if ($salt == $key)
			{
				$user->setIsActive(true);
				$user->save();
				$tutor = new Tutor();
				$tutor->setUserId($user->getId());
				$tutor->save();

				return sfView::SUCCESS; //User activated
			}
			else
			{
				return 'ErrorKey'; //Wrong key
			}
		}
		else
		{
			$user = sfGuardUserPeer::retrieveByUsername($login);

			if ($user)
			{
				return 'UserActivated'; //User already activated
			}
			else
			{
				return 'ErrorUser'; //No such user found
			}
		}
	}

	public function executeView()
	{
		$uname = $this->getRequestParameter('username');
		$this->enableedit = false;

		if ($uname == 'me')
		{
			if ($this->getUser()->isAuthenticated() && $this->getUser()->hasGroup('tutor'))
			{
				$this->userx = $this->getUser()->getGuardUser();
				$this->tutor = $this->userx->getTutors();
				$this->subjects = $this->tutor[0]->getSubjects();
				$this->servicearea = $this->tutor[0]->getServiceArea();
				$this->availabilities = $this->tutor[0]->getAvailabilities();

				$this->enableedit = true;
			}
			else
			{
				//$this->forward404();
				$this->redirect('@sf_guard_signin');
			}
		}
		else
		{
			$this->userx = sfGuardUserPeer::retrieveByUsername($uname);
			//$this->forward404Unless($this->userx);
			if(!($this->userx))
			{
				$this->redirect('@unknownuser');
			}
			$this->tutor = $this->userx->getTutors();
			$this->subjects = $this->tutor[0]->getSubjects();
			$this->servicearea = $this->tutor[0]->getServiceArea();
			$this->availabilities = $this->tutor[0]->getAvailabilities();

			if ($this->getUser()->isAuthenticated() && $this->getUser()->hasGroup('tutor'))
			{
				$this->enableedit = strtolower($this->getUser()->getGuardUser()->getUsername()) == strtolower($uname);
			}
		}

		return sfView::SUCCESS;
	}

	public function executeEdit()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			$this->userx = $this->getUser()->getGuardUser();
			$this->tutor = $this->userx->getTutors();
			
			if(!(is_null($this->tutor[0]->getServiceArea())))
			{
				$this->servicearea = $this->tutor[0]->getServiceArea()->getId();
			}
			else
			{
				$this->servicearea = null;
			}
	            $this->serviceareas= ServiceAreaPeer::doSelect(new Criteria());

			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Save profile
			$userx = $this->getUser()->getGuardUser();
			$tutor = $userx->getTutors();
			$tutor[0]->setFirstname($this->getRequestParameter('firstname'));
			$tutor[0]->setLastname($this->getRequestParameter('lastname'));
			$tutor[0]->setRates($this->getRequestParameter('rates'));
			if(!(is_null($this->getRequestParameter('additionalinfo'))))
			{
				$tutor[0]->setAdditionalinfo($this->getRequestParameter('additionalinfo'));
			}
			$tutor[0]->setServiceareaId($this->getRequestParameter('servicearea'));
			$tutor[0]->setAvailabilities($this->getRequestParameter('associated_availabilities'),$tutor[0]->getId());
			$tutor[0]->setSubjects($this->getRequestParameter('associated_subjects'),$tutor[0]->getId());
			$tutor[0]->save();

			$this->redirect('@viewuser');
		}
	}

	public function handleErrorEdit()
	{
		$this->userx = $this->getUser()->getGuardUser();
		$this->tutor = $this->userx->getTutors();
		$this->servicearea = $this->tutor[0]->getServiceArea()->getId();
            $this->serviceareas= ServiceAreaPeer::doSelect(new Criteria());

		return sfView::SUCCESS;
	}

	public function executePassword()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Save password
			$user = $this->getUser()->getGuardUser();
			$user->setPassword($this->getRequestParameter('password'));
			$user->save();
			$this->redirect('@viewuser');
		}
	}

	public function handleErrorPassword()
	{
		return sfView::SUCCESS;
	}
}